package com.zwr.pattern.workerthread;

import sun.security.util.math.SmallValue;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author Administrator
 */
public class TestDeadLock {

    static final List<String> MENU = Arrays.asList("菜品1", "菜品2", "菜品3", "菜品4");
    static Random random = new Random();

    static String cooking() {
        int i = random.nextInt(MENU.size());
        return MENU.get(i);
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(() -> {
            System.out.println("处理点餐");
            Future<String> f = executorService.submit(() -> {
                System.out.println("做菜");
                return cooking();
            });
            try {
                System.out.println("上菜: {}" + f.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });

        executorService.execute(() -> {
            System.out.println("处理点餐...");
            Future<String> f = executorService.submit(() -> {
                System.out.println("做菜");
                return cooking();
            });
            try {
                System.out.println("上菜: {}" + f.get());
            } catch (InterruptedException | ExecutionException e) {

                e.printStackTrace();
            }
        });

    }
}
